package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplus.model.domain.Building;
import com.example.mybatisplus.model.domain.Dot;
import com.example.mybatisplus.service.BuildingService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.AreaService;
import com.example.mybatisplus.model.domain.Area;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author zzy
 * @since 2024-06-24
 * @version v1.0
 */
@RestController
@RequestMapping("/api/area")
public class AreaController {

    private final Logger logger = LoggerFactory.getLogger( AreaController.class );

    @Autowired
    private AreaService areaService;


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Area  area =  areaService.getById(id);
        return JsonResponse.success(area);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        areaService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateArea(Area area) throws Exception {
        areaService.updateById(area);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Area
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Area area) throws Exception {
        areaService.save(area);
        return JsonResponse.success(null);
    }


    @PostMapping("/area_all")
    public JsonResponse area_all(@RequestBody Dot dot){
        List<Area> areaList = areaService.selectAreaPage(dot);

        return JsonResponse.success(areaList, "Select area success.").addOtherData("num", areaList.size());
    }
    @PostMapping("/area_add")
    public JsonResponse area_add(@RequestBody Area area){
        area.setIsDeleted(0);
        LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Area::getAddress, area.getAddress()).eq(Area::getName, area.getName());
        Area area1 = areaService.getOne(wrapper);
        if(area1.getIsDeleted() == 0){
            return JsonResponse.failure(String.format("Area \"%s\" has already existed.", area.getAddress()));
        }
        else {
            Area area2 = new Area()
            .setId(area1.getId())
            .setIsDeleted(0)
            .setModifyTime(LocalDateTime.now())
            .setCreatedTime(LocalDateTime.now());
            areaService.updateById(area2);
        }
        areaService.save(area);
        return JsonResponse.success(true, "Area adding success.");
    }
    @DeleteMapping("/area_delete")
    public JsonResponse area_delete(int AreaID){
        Area area = new Area();
        area.setId((long)AreaID);

        area.setIsDeleted(1);
        areaService.updateById(area);
        return JsonResponse.success(true, "Area delete success.");
    }
    @PostMapping("/area_modify")
    public JsonResponse area_modify(@RequestBody Area area){
        area.setModifyTime(LocalDateTime.now());
        Boolean b = areaService.updateById(area);
        return JsonResponse.success(b, "Area modify success");
    }
    @PostMapping("/select_area_by_areaname")
    public JsonResponse select_area_by_areaname(@RequestBody Dot dot){
        List<Area> areaList = areaService.selectAreaPageByName(dot);
        return JsonResponse.success(areaList, "Area select by name success");
    }

    @PostMapping("/get_total_people_by_AreaID")
    public JsonResponse get_total_people_by_AreaID(long AreaID){
        int count = areaService.get_total_people_by_AreaID(AreaID);
        return JsonResponse.success(count, "Area get totel people by ID success");
    }
}

